package com.hexacta.hrs.sandbox.pr.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.hexacta.hrs.sandbox.pr.model.Category;
import com.hexacta.hrs.sandbox.pr.model.Person;
import com.hexacta.hrs.sandbox.pr.model.Profile;
import com.hexacta.hrs.sandbox.pr.model.Review;
import com.hexacta.hrs.sandbox.pr.service.CategoryService;
import com.hexacta.hrs.sandbox.pr.service.PersonService;
import com.hexacta.hrs.sandbox.pr.service.ReviewService;
import com.opensymphony.xwork2.ActionSupport;

@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class ReviewAction extends ActionSupport
{
	private static final long serialVersionUID = 1L;
	String date;
	List<Category> categories;
	List<Review> reviews;
	
	List<Person> listPerson;
	
	String selectedPerson;

	String selectedCategory;
	String recommendedRevisorsQ;
	String hour;
	
	String selectedReview;
	String addtype;
	String selectedRevisor;

	@Autowired
	CategoryService categoryService;
	@Autowired
	ReviewService reviewService;
	@Autowired
	PersonService personService;
	
	public String getSelectedRevisor() {
		return selectedRevisor;
	}

	public void setSelectedRevisor(String selectedRevisor) {
		this.selectedRevisor = selectedRevisor;
	}

	public List<Person> getListPerson() {
		return listPerson;
	}

	public void setListPerson(List<Person> listPerson) {
		this.listPerson = listPerson;
	}

	public String getSelectedPerson() {
		return selectedPerson;
	}

	public void setSelectedPerson(String selectedPerson) {
		this.selectedPerson = selectedPerson;
	}

	public String getAddtype() {
		return addtype;
	}

	public void setAddtype(String addtype) {
		this.addtype = addtype;
	}

	public String getSelectedReview() {
		return selectedReview;
	}

	public void setSelectedReview(String selectedReview) {
		this.selectedReview = selectedReview;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public String getHour() {
		return hour;
	}

	public void setHour(String hour) {
		this.hour = hour;
	}

	public List<Review> getReviews() {
		return reviews;
	}

	public void setReviews(List<Review> reviews) {
		this.reviews = reviews;
	}
	
	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public String getSelectedCategory() {
		return selectedCategory;
	}

	public void setSelectedCategory(String selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

	public String getRecommendedRevisorsQ() {
		return recommendedRevisorsQ;
	}

	public void setRecommendedRevisorsQ(String recommendedRevisorsQ) {
		this.recommendedRevisorsQ = recommendedRevisorsQ;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public ReviewService getReviewService() {
		return reviewService;
	}

	public void setReviewService(ReviewService reviewService) {
		this.reviewService = reviewService;
	}

	public String execute()
	{
	    return SUCCESS;
	}
	
	public String create() {
		//Es el primer pedido, load the view
		this.setCategories(this.getCategoryService().listAll());
		return SUCCESS;
	}
	
	public String save() {
		//Validacion
		this.Reviewvalidate();
		
		if( this.hasFieldErrors() ) {
			//TODO esto deberia hacerse con validate de struts
			//tengo que llenar otra vez las listas para la vista
			this.setCategories(this.getCategoryService().listAll());
			return "VALIDATE";
		}
		
		Date parsedDate = null;
		try {
			parsedDate = DateUtil.getDate(date,hour);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Category category = this.categoryService.findCategoryByName(selectedCategory).get(0);
		this.getReviewService().addReview(parsedDate,category,Integer.parseInt(recommendedRevisorsQ));
			
		return "LIST";
	}
	
	public String list() {
		List<Review> allReviews = this.getReviewService().listAll();
		Collections.sort(allReviews);
		this.setReviews(allReviews);
		return SUCCESS;
	}
	
	public String remove() {
		return SUCCESS;
	}

	public void Reviewvalidate() {
		//Validate
	}
	
	public String populate() {
		List<Person> persons = this.getPersonService().listAll();
		
		listPerson = new ArrayList<Person>();
		
		for( Person person : persons ) {
			for( Profile profile : person.getProfiles() ) {
				if( profile.getName().equals("MODERATOR") && this.addtype.equals("mod") )
					listPerson.add(person);
				else if( profile.getName().equals("AUTHOR") && this.addtype.equals("author") )
					listPerson.add(person);
				else if( profile.getName().equals("REVISOR") && this.addtype.equals("revisor") )
					listPerson.add(person);
			}
		}
		
		return SUCCESS;
	}

	public String savePopulate() {
		Review review = this.getReviewService().findReviewById(Long.parseLong(selectedReview)).get(0);
		Person person2add = this.getPersonService().findPersonByName(selectedPerson).get(0);
		this.populate();
		
		if( this.getAddtype().equals("mod") ) {
			
			if( review.getMod() != null ) {
				this.addFieldError("selectedPerson", "Moderator already setted");
				return "VALIDATE";
			} else {
				try {
					review.setMod(person2add);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else if( this.getAddtype().equals("author") ) {
					
			if( review.getAuthors().size() == 2 ) {
				this.addFieldError("selectedPerson", "2 authors already setted");
				return "VALIDATE";
			} else {
				
				if( review.getAuthors().contains(person2add) ) {
					this.addFieldError("selectedPerson", "Author already setted for this review");
					return "VALIDATE";
				}
				try {
					review.addAuthor(person2add);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else if( this.getAddtype().equals("revisor") ) {
			
			if( review.getRevisors().contains(person2add) ) {
				this.addFieldError("selectedPerson", "Revisor already setted for this review");
				return "VALIDATE";
			}
			
			try {
				review.addRevisor(person2add);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		this.getReviewService().update(review);
		
		return "LIST";
	}
	
	public String close() {	
		Review review = this.getReviewService().findReviewById(Long.parseLong(selectedReview)).get(0);
		
		review.setRecommendedRevisorsQ(-1);
		
		this.getReviewService().update(review);
		return "LIST";
	}
	
	public String deleteRevisor() {
		Review review = this.getReviewService().findReviewById(Long.parseLong(selectedReview)).get(0);
		Person person = this.getPersonService().findPersonByName(selectedRevisor).get(0);
		
		review.getRevisors().remove(person);
		
		this.getReviewService().update(review);
		
		return "LIST";
	}
}
